import { Component, OnInit ,Input} from '@angular/core';
import { QuestionsService } from '../service/questions.service';
import { Question } from '../question';
import { ActivatedRoute } from '@angular/router';
@Component({
  selector: 'app-answer-question',
  templateUrl: './answer-question.component.html',
  styleUrls: ['./answer-question.component.css']
})
export class AnswerQuestionComponent implements OnInit {

  question: Question;
  num: number;//数据库里的题号(1-10为一模块的题目，11-20为另一模块的，以此类推)
  questionNum: number; //题号(1-10)
  choice: String; //答题者选的答案A、B、C、D
  showAnalysis: boolean = false; //决定题目解析和解析相关的图片什么时候显示出来
  tip: string;
  showTip: boolean = false; //决定tip什么时候显示出来
  rightAnswer: String; //正确答案

  constructor(private questionsService: QuestionsService, private route: ActivatedRoute) { }

  getOption(value: any){
    this.choice = value;
 }

  onClickToBack() {
    if(this.questionNum > 1){
       this.num = this.num - 1;
       this.questionNum = this.questionNum - 1;
       this.getData(this.num);
       if(this.choice != undefined){
         this.tip = "题目分析如下";
         this.showAnalysis = true;
         this.showTip = true;
      }
    }else if(this.questionNum == 1){
      alert("已到达第一题，无上一题");
    }
  }

  onClickToSubmit() {
    if(this.question.data["answer"] == 1){
      this.rightAnswer = 'A';
    }else if(this.question.data["answer"] == 2){
      this.rightAnswer = 'B';
    }else if(this.question.data["answer"] == 4){
      this.rightAnswer = 'C';
    }else if(this.question.data["answer"] == 8){
      this.rightAnswer = 'D';
    }
    if(this.choice == undefined){
      this.tip = "请作答后再查看解析";
      //alert("请作答后再查看解析");
    }else{
      if(this.rightAnswer == this.choice){
        this.tip = "正确答案是 " + this.rightAnswer +" 恭喜您回答正确!";
      }else{
        this.tip = "正确答案是 " + this.rightAnswer +" 非常遗憾，回答错误!";
      }
      this.showAnalysis = true;
    }
    this.showTip = true;
    //alert(this.question.data['answer']);
  }

  onClickToNext() {
    if(this.question.data["answer"] == 1){
      this.rightAnswer = 'A';
    }else if(this.question.data["answer"] == 2){
      this.rightAnswer = 'B';
    }else if(this.question.data["answer"] == 4){
      this.rightAnswer = 'C';
    }else if(this.question.data["answer"] == 8){
      this.rightAnswer = 'D';
    }
    if(this.choice != undefined && this.showAnalysis == false){
      if(this.choice == this.rightAnswer){
        this.tip = "正确答案是 " + this.rightAnswer +" 恭喜您回答正确!";
        alert("恭喜您回答正确!");
      }else{
        this.tip = "正确答案是 " + this.rightAnswer +" 非常遗憾，回答错误!";
        alert("非常遗憾，回答错误，正确答案是 " + this.rightAnswer);
      }
    }
    if(this.questionNum < 10){
      this.num = this.num + 1;
      this.questionNum = this.questionNum + 1;
      this.getData(this.num);
      this.showAnalysis = false;
      this.showTip = false;
   }else if(this.questionNum == 10){
      alert("已到达第十题，无下一题");
    }
  }

  ngOnInit() {
    this.num = +this.route.snapshot.paramMap.get('num');
    this.getData(this.num)
    this.questionNum = this.num % 10; 
    //alert(this.question.data['answer']); alert和if语句在ngOnInit()无用
  }

  getData(num): void{
    this.questionsService.getData(num)
      .subscribe(res => {
        this.question = res;
        console.log(this.question)
        //console.log(res.data);
      });
  }

}
